package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.admin.dto.RechargeAndSwapDto;
import com.letoken.platform.repo.admin.req.user.QueryAppKolListReq;
import com.letoken.platform.repo.admin.req.user.QueryTeamAchievementReq;
import com.letoken.platform.repo.admin.res.system.UserCommunityKolRes;
import com.letoken.platform.repo.admin.res.user.QueryAppKolListRes;
import com.letoken.platform.repo.admin.res.user.QueryTeamAchievementRes;
import com.letoken.platform.repo.app.po.AppUserTeam;
import com.letoken.platform.repo.app.req.team.ConsumerValueHistoryReq;
import com.letoken.platform.repo.app.res.team.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * Le账户-团队统计信息(KOL) Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2022-12-13
 */
public interface AppUserTeamMapper extends BaseMapper<AppUserTeam> {
    AppUserTeam getUserLevel(@Param("userId")Integer userId);

    AppUserTeam getUserMaxValue(@Param("inviterId") Integer inviterId);

    BigDecimal getSumValue(@Param("inviterId") Integer inviterId, @Param("userId")Integer userId);

    Page<QueryAppKolListRes> queryUserTeamList(@Param("page") Page<QueryAppKolListRes> page, @Param("req") QueryAppKolListReq req);


    @Select("SELECT t.consumer_value FROM app_user_team t   WHERE t.user_id = #{userId}")
    BigDecimal queryConsumerValue(@Param("userId") Integer userId);

    QueryTeamRes queryTeamInfo(@Param("userId") Integer userId);

    Page<QueryTeamMemberRes> queryTeamMembers(@Param("page") Page<QueryTeamMemberRes> page, @Param("userId") Integer userId);

    List<QueryTeamLevelInfoRes> queryTeamLevelInfo();

    QueryTeamLevelRes queryTeamLevel(@Param("userId") Integer userId);

    AppUserTeam getMAXTeamValue(@Param("inviterId") Integer inviterId);


    List<AppUserTeam> getMAXTeamValueSize(@Param("inviterId") Integer inviterId);

    List<AppUserTeam> getMAXFundTeamValueSize(@Param("inviterId") Integer inviterId);

    List<AppUserTeam> getMAXMallTeamValueSize(@Param("inviterId") Integer inviterId);


    Integer getSUMTeamValue(@Param("inviterId") Integer inviterId,@Param("userId")Integer userId);
    Integer getSUMFundTeamValue(@Param("inviterId") Integer inviterId,@Param("userId")Integer userId);

    BigDecimal getSUMMallTeamValue(@Param("inviterId") Integer inviterId,@Param("userId")Integer userId);

    Integer getCount(@Param("inviterId") Integer inviterId,@Param("userId")Integer userId);

    /**
     * 团队业绩和已流通
     */
    QueryTeamAchievementRes circulationAndAmount(@Param("req") QueryTeamAchievementReq req);

    /**
     * 平台互转(LET)
     */
    BigDecimal queryPlatformTransferLet(@Param("req") QueryTeamAchievementReq req);

    RechargeAndSwapDto queryRechargeAndSwap(@Param("req") QueryTeamAchievementReq req);

    /**
     * let闪兑usdt
     */
    BigDecimal queryLetSwapUsdt(@Param("req") QueryTeamAchievementReq req);

    /**
     * 提现usdt
     */
    BigDecimal queryWithdrawUsdt(@Param("req") QueryTeamAchievementReq req);


    Integer updateTeamMaxAccelerate();

    List<Integer> getMallUserId();

    BigDecimal getAmount(@Param("userId")Integer userId);

    void updateAchievement(@Param("amount")BigDecimal amount,@Param("userId")Integer userId);

    BigDecimal issueTeamValue(@Param("userId")Integer userId);


    @Select("select amount,create_time,type from app_consumer_history where user_id = #{req.userId} order by create_time desc ")
    Page<ConsumerValueHistoryRes> consumerValueHistoryRes(@Param("page") Page<ConsumerValueHistoryRes> page, @Param("req") ConsumerValueHistoryReq req);

    @Select("select user_id userId,user_achievement userAchievement,team_sum_value teamSumValue,mall_achievement mallCchievement,team_mall_achievement teamMallAchievement,fund_achievement fundCchievement,team_fund_achievement teamFundAchievement from app_user_team where user_id = #{userId}")
   AppUserTeam getAchievement(@Param("userId")Integer userId);

    @Select("select user_id userId,team_member teamMembers,user_achievement userAchievement,team_sum_value teamSumValue,mall_achievement mallAchievement,team_mall_achievement teamMallAchievement,fund_achievement fundAchievement,team_fund_achievement teamFundAchievement from app_user_team where user_id =#{userId} or inviter_id =#{userId} order by user_id")
    List<QueryAppKolListRes> getTeamList(@Param("userId") Integer userId);


    @Select("select * from app_user_team where user_id = #{userId} for update")
    AppUserTeam selectForUpdate(@Param("userId") Integer userId);

    @Select("select * from app_user_team where user_id = #{userId}")
    AppUserTeam selectByUserId(@Param("userId") Integer userId);

    @Select("select user_id from app_user_team where level != 0 ")
    HashSet<Integer> listInt();

    @Select("select user_id from app_user_team where level = 2 ")
    HashSet<String> operatingCenters();

    @Select("select user_id from app_fund_level_info where level >= 9 ")
    HashSet<String> partners();

    Page<UserCommunityKolRes> page(@Param("page") Page page, @Param("userId") Integer userId, @Param("level") Integer level);
}
